package com.yonyougov.devops.cruxcisupport.rolemenu;

import com.yonyougov.devops.cruxcisupport.rolemenu.dto.AddMulRoleMenuDto;
import com.yonyougov.devops.cruxcisupport.rolemenu.dto.AddMulRoleMenuRequestDto;
import com.yonyougov.devops.cruxcisupport.rolemenu.dto.DeleteMulRoleMenuRequestDto;
import com.yonyougov.devops.cruxcisupport.rolemenu.dto.RoleMenuPageLikeRequestDto;
import com.yonyougov.devops.cruxcisupport.rolemenu.vo.RoleMenuPageLikeDto;
import com.yonyougov.devops.cruxcisupport.rolemenu.vo.RoleMenuPageLikeVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(value = "角色菜单关联模块", tags = "角色菜单关联模块")
@RestController
@RequestMapping("/api/v2/rolemenu")
public class RoleMenuController extends CrudController<RoleMenuEntity, BigInteger> {

    private RoleMenuService roleMenuService;

    RoleMenuController(RoleMenuService roleMenuService) {
        this.roleMenuService = roleMenuService;
    }

    @Override
    public BaseService<RoleMenuEntity, BigInteger> getService() {
        return roleMenuService;
    }

    //<!--用户自定义代码开始-->

    @ApiModelProperty(value = "角色管理关联菜单调用", notes = "批量新增关联")
    @PostMapping(value = "/rm/add/batch")
    public BaseResponseDto addBatchRoleMenu(@RequestBody @Valid AddMulRoleMenuRequestDto request) {
        AddMulRoleMenuDto addMulRoleMenuDto = AddMulRoleMenuDto.builder().build();
        BeanUtils.copyProperties(request, addMulRoleMenuDto);

        roleMenuService.addMulRoleMenu(addMulRoleMenuDto);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiModelProperty(value = "角色管理关联菜单调用", notes = "批量删除关联")
    @PostMapping(value = "/rm/delete/batch/ids")
    @SneakyThrows
    public BaseResponseDto DeleteBatchRoleEnvByIds(@RequestBody @Valid DeleteMulRoleMenuRequestDto request) {
        roleMenuService.deleteBatchRoleMenuByIds(request.getIds());

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiModelProperty(value = "角色管理关联菜单调用", notes = "可选择根据角色id和菜单名称分页模糊查询关联")
    @PostMapping(value = "/rm/page/like")
    public BaseResponseDto findRoleMenuPageLIke(@RequestBody @Valid RoleMenuPageLikeRequestDto request) {
        RoleMenuPageLikeDto roleMenuPageLikeDto = new RoleMenuPageLikeDto();
        BeanUtils.copyProperties(request, roleMenuPageLikeDto);

        Pagination<RoleMenuPageLikeVo> pagination = roleMenuService.findRoleMenuPageLike(roleMenuPageLikeDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }


    //<!--用户自定义代码结束-->
}
